home *** CD-ROM | disk | FTP | other *** search
/ BMUG Revelations / BMUG Revelations.toast / Utilities / Random / Commodore 64c / SOURCE / HardDrive.c < prev    next >
Text File  |  1994-02-24  |  4KB  |  146 lines

  1. /*
  2.     Commodore 64 Emulator v0.1      Earle F. Philhower III 
  3.     Copyright (C) 1993-4            (st916w9r@dunx1.ocs.drexel.edu)
  4.  
  5.     This program is free software; you can redistribute it and/or modify
  6.     it under the terms of the GNU General Public License as published by
  7.     the Free Software Foundation; either version 2 of the License, or
  8.     (at your option) any later version.
  9.  
  10.     This program is distributed in the hope that it will be useful,
  11.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13.     GNU General Public License for more details.
  14.  
  15.     You should have received a copy of the GNU General Public License
  16.     along with this program; if not, write to the Free Software
  17.     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18. */
  19. #include "ProcessorTypes.h"
  20. #include "MemoryCalls.h"
  21. #include "Serial.h"
  22. #include "HardDrive.h"
  23.  
  24. int PetConv(int c);
  25. void PetConvString(Str32 c);
  26. static byte ReadMac(unsigned char *data, int secondary);
  27. static byte WriteMac(unsigned char data, int secondary);
  28. static byte OpenMac(char *name, int length, int secondary);
  29. static byte CloseMac(int secondary);
  30. static byte PutByte(byte data, int secondary);
  31. static byte GetByte(byte *data, int secondary);
  32.  
  33. enum { NOTINUSE=0, WRITE, READ };
  34.  
  35. typedef struct {
  36.     short fNum;
  37.     byte mode;
  38.     byte *buffer;
  39.     int bufOff;
  40.     long bufLen;
  41. } FSInfo;
  42. static FSInfo fsInfo[2];
  43. static short dirID=0;
  44.  
  45. static byte PutByte(byte data, int secondary)
  46. {
  47.     FSInfo *fs=&(fsInfo[secondary]);
  48.     
  49.     if (fs->bufLen>=256) {
  50.         if (FSWrite(fs->fNum, &(fs->bufLen), fs->buffer)!=noErr) return kSerialError;
  51.         fs->bufLen=0; }
  52.     fs->buffer[fs->bufLen++]=data;
  53.     return kSerialOK;
  54. }
  55.  
  56. static byte GetByte(byte *data, int secondary)
  57. {
  58.     FSInfo *fs=&(fsInfo[secondary]);
  59.  
  60.     if (fs->bufOff>=fs->bufLen) {
  61.         fs->bufLen=256;
  62.         FSRead(fs->fNum, &(fs->bufLen), fs->buffer);
  63.         if (fs->bufLen==0) return kSerialEOF;
  64.         fs->bufOff=0;}
  65.     *data=fs->buffer[fs->bufOff++];
  66.     return kSerialOK;
  67. }
  68.     
  69. void HardInitialize(void)
  70. {
  71.     fsInfo[0].buffer=GetMemory(256);
  72.     fsInfo[1].buffer=GetMemory(256);
  73.     fsInfo[0].mode=fsInfo[1].mode=NOTINUSE;
  74.  
  75.     AddSerialDevice(9, ReadMac, WriteMac, OpenMac, CloseMac, NULL);
  76. }
  77.  
  78. int PetConv(int c)
  79. {
  80.     switch (c&0xe0) {
  81.         case 0x40:
  82.         case 0x60: return (c^0x20); }
  83.     return (c);
  84. }
  85.  
  86. void PetConvString(Str32 c)
  87. {
  88.     int i;
  89.     for (i=1; i<c[0]+1; i++) c[i]=PetConv(c[i]);
  90. }
  91.  
  92. static byte WriteMac(unsigned char data, int secondary)
  93. {
  94.     if (fsInfo[secondary].mode!=WRITE) return kFloppyError;
  95.     return PutByte(data, secondary);
  96. }
  97.  
  98. static byte ReadMac(unsigned char *data, int secondary)
  99. {
  100.     if (fsInfo[secondary].mode!=READ) return kFloppyError;
  101.     return GetByte(data, secondary);
  102. }
  103.  
  104. static byte OpenMac(char *name, int length, int secondary)
  105. {
  106.     Str32 tmp;
  107.     if (fsInfo[secondary].mode!=NOTINUSE) return kFloppyError;
  108.     if ((secondary<0)||(secondary>=2)) return kFloppyError;    
  109.  
  110.     BlockMove(name, tmp+1, length);
  111.     tmp[0]=length;
  112.     PetConvString(tmp);
  113.  
  114.     if ((fsInfo[secondary].mode=(secondary==1?WRITE:READ))==WRITE)
  115.         Create(tmp, dirID, 'C64E', 'TEXT');
  116.     if (FSOpen(tmp, dirID, &(fsInfo[secondary].fNum))!=noErr) return kFloppyError;
  117.     fsInfo[secondary].bufLen=fsInfo[secondary].bufOff=0;
  118.     return kSerialOK;
  119. }
  120.  
  121. static byte CloseMac(int secondary)
  122. {
  123.     switch(fsInfo[secondary].mode) {
  124.         case WRITE:
  125.             if (fsInfo[secondary].bufLen>0)
  126.                 FSWrite(fsInfo[secondary].fNum, &fsInfo[secondary].bufLen,
  127.                     fsInfo[secondary].buffer);
  128.         case READ: FSClose(fsInfo[secondary].fNum); return kSerialOK;
  129.         default: return kSerialError; }
  130. }
  131.  
  132. void HardChangeDirectory(void)
  133. {
  134. /*
  135.     Str255 str;
  136.     StandardFileReply sfReply;
  137.     Point pt={60,60};
  138.     
  139.     StandardGetFolder(pt, "\pSelect New Directory For Drive 9:", &sfReply);
  140.     if (sfReply.sfGood==false) return;
  141.     dirID=sfReply.sfFile.parID;
  142.     NumToString(dirID, str);
  143.     DebugStr(str);
  144. */
  145. }
  146.